package com.muc.view;

import com.muc.handler.MainViewHandler;
import com.muc.req.CarRequest;
import com.muc.res.TableDTO;
import com.muc.service.CarService;
import com.muc.service.impl.CarServiceImpl;
import com.muc.util.DimensionUtil;
import com.muc.view.ext.MainViewTable;
import com.muc.view.ext.MainViewTableModel;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Vector;

public class MainView extends JFrame{
//    JPanel northPanel = new JPanel();
    JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JButton addBtn = new JButton("增加");
    JButton updateBtn = new JButton("修改");
    JButton delBtn = new JButton("删除");
    JTextField searchTxt = new JTextField(15);
    JButton searchBtn = new JButton("查询");

    JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

    JButton preBtn = new JButton("上一页");
    JButton nextBtn = new JButton("下一页");

    //放置数据的
    MainViewTable mainViewTable = new MainViewTable();

    private int pageNow = 1;//当前页数 默认第一页
    private int pageSize = 10;//一页显示多少条记录

    MainViewHandler mainViewHandler;

    private boolean isAdd = false;
    private boolean isUpdate = false;
    private boolean isDel = false;
    private boolean isSearch = false;
    private boolean isPrePage = false;
    private boolean isNextPage = false;
    //private
    public boolean isAdd() {
        return isAdd;
    }

    public void setAdd(boolean add) {
        isAdd = add;
    }

    public boolean isUpdate() {
        return isUpdate;
    }

    public void setUpdate(boolean update) {
        isUpdate = update;
    }

    public boolean isDel() {
        return isDel;
    }

    public void setDel(boolean del) {
        isDel = del;
    }

    public boolean isSearch() {
        return isSearch;
    }

    public void setSearch(boolean search) {
        isSearch = search;
    }

    public boolean isPrePage() {
        return isPrePage;
    }

    public void setPrePage(boolean prePage) {
        isPrePage = prePage;
    }

    public boolean isNextPage() {
        return isNextPage;
    }

    public void setNextPage(boolean nextPage) {
        isNextPage = nextPage;
    }

    public MainView(){
        super("主界面-租车信息管理");
        Rectangle rectangle = DimensionUtil.getBounds();
        //调整每页显示的数量
        pageSize = Math.floorDiv(rectangle.height,40);
        Container contentPane = getContentPane();
        mainViewHandler = new MainViewHandler(this);
        //放置北边的组件
        layoutNorth(contentPane);
        //设置中间的table
        layoutCenter(contentPane);
        //放置南边的组件
        layoutSouth(contentPane);
        //设定窗体图标
        URL resource = MainView.class.getClassLoader().getResource("cage_ganda-removebg-preview.png");
        Image image = new ImageIcon(resource).getImage();
        setIconImage(image);

        //根据屏幕大小设置主界面大小
        setBounds(rectangle);
        //设置窗体完全充满整个屏幕的可见大小
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        //居中
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //大小不可改变
        setResizable(true);
        setVisible(true);
    }

    private void layoutCenter(Container contentPane) {

        TableDTO dto = getTableDTO();
        Vector<Vector<Object>> data = dto.getData();

        //滚动面板 mainViewTable放在上的话，默认是不展示列头的，需要特殊设置。放在JScrollPane上面
        // 默认是展示列头的
        MainViewTableModel mainViewTableModel = MainViewTableModel.assembleModel(data);
        //把jtable和model关联
        mainViewTable.setModel(mainViewTableModel);
        mainViewTable.renderRule();
        JScrollPane jScrollPane = new JScrollPane(mainViewTable);

        contentPane.add(jScrollPane,BorderLayout.CENTER);
        showPreNext(dto.getTotalCount());
    }

    /*private Vector<Vector<Object>> getVectors() {
        //通过数据库更新信息
        CarService carService = new CarServiceImpl();
        CarRequest request = new CarRequest();
        request.setPageNow(pageNow);//点击下一页pageNow改变
        request.setPageSize(pageSize);
        request.setSearchKey(searchTxt.getText().trim());

        //返回sql语句查询结果
        TableDTO tableDTO = carService.retrieveCars(request);
        Vector<Vector<Object>> data = tableDTO.getData();
        return data;
    }*/
    private TableDTO getTableDTO() {
        //通过数据库更新信息
        CarService carService = new CarServiceImpl();
        CarRequest request = new CarRequest();
        request.setPageNow(pageNow);//点击下一页pageNow改变
        request.setPageSize(pageSize);
        request.setSearchKey(searchTxt.getText().trim());

        //返回sql语句查询结果
        TableDTO tableDTO = carService.retrieveCars(request);
        return tableDTO;
    }

    //设置上一页、下一页是否可见
    private void showPreNext(int totalCount){
        if(pageNow==1){
            preBtn.setVisible(false);
        }else {
            preBtn.setVisible(true);
        }
        int pageCount = 0;//总页数
        if(totalCount%pageSize==0){
            pageCount = totalCount/pageSize;
        }else {
            pageCount = totalCount/pageSize+1;
        }
        if(pageNow == pageCount){//到达最后一页，就没有下一页可言
            nextBtn.setVisible(false);
        }else {
            nextBtn.setVisible(true);
        }
    }
    private void layoutSouth(Container contentPane) {
        preBtn.addActionListener(mainViewHandler);
        nextBtn.addActionListener(mainViewHandler);
        southPanel.add(preBtn);
        southPanel.add(nextBtn);

        contentPane.add(southPanel,BorderLayout.SOUTH);
    }
    private void layoutNorth(Container contentPane) {
        //增加时间监听
        addBtn.addActionListener(mainViewHandler);
        updateBtn.addActionListener(mainViewHandler);
        delBtn.addActionListener(mainViewHandler);
        searchBtn.addActionListener(mainViewHandler);
        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel,BorderLayout.NORTH);
    }


    public static void main(String[] args) {
        new MainView();
    }


    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void reloadTable() {
        TableDTO dto = getTableDTO();
        Vector<Vector<Object>> data = dto.getData();
        MainViewTableModel.updateModel(data);
        //再次进行渲染
        mainViewTable.renderRule();
        showPreNext(dto.getTotalCount());
    }

    public int[] getSelectedCarIds(){
        int[] selectedRows = mainViewTable.getSelectedRows();
        int[] ids = new int[selectedRows.length];
        for(int i=0;i< selectedRows.length;i++){
            int rowIndex = selectedRows[i];
            Object idObj = mainViewTable.getValueAt(rowIndex, 0);
            ids[i] = Integer.valueOf(idObj.toString());
        }
        return ids;
    }
}
